Passed
Push — master ( 5ad395...445144 )
by Rafael S.
01:14
created

to-bytes.js ➔ makeBigEndian   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 5
rs 9.4285
1
/*
2
 * to-bytes: bytes to numbers and strings.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
const intBits = require("int-bits");
0 ignored issues
show
Unused Code introduced by
The constant intBits seems to be never used. Consider removing it.
Loading history...
8
const pad = require("../src/byte-padding.js");
9
const endianness = require("endianness");
10
const writer = require("../src/write-bytes.js");
11
const bitDepths = require("../src/bit-depth.js");
12
13
/**
14
 * Turn numbers and strings to bytes.
15
 * @param {!Array<number>|number|string} values The data.
16
 * @param {number} bitDepth The bit depth of the data.
17
 *   Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64.
18
 * @param {Object} options The options:
19
 *   - "float": True for floating point numbers. Default is false.
20
 *       This option is available for 16, 32 and 64-bit numbers.
21
 *   - "base": The base of the output. Default is 10. Can be 2, 10 or 16.
22
 *   - "char": If the bytes represent a string. Default is false.
23
 *   - "be": If the values are big endian. Default is false (little endian).
24
 *   - "buffer": If the bytes should be returned as a Uint8Array.
25
 *       Default is false (bytes are returned as a regular array).
26
 * @return {!Array<number>|Uint8Array} the data as a byte buffer.
27
 */
28
function toBytes(values, bitDepth, options={}) {
29
    if (!options.char && typeof values != "string") {
30
        values = turnToArray(values);
31
    }
32
    let base = 10;
33
    if ("base" in options) {
34
        base = options.base;
35
    }
36
    let bytes = writeBytes(values, options.char, options.float, bitDepth);
37
    makeBigEndian(bytes, options.be, bitDepth);
38
    outputToBase(bytes, bitDepth, base);
39
    if (options.buffer) {
40
        bytes = new Uint8Array(bytes);
41
    }
42
    return bytes;
43
}
44
45
/**
46
 * Make a single value an array in case it is not.
47
 * @param {!Array<number>|number|string} values The value or values.
48
 * @return {!Array<number>}
49
 */
50
function turnToArray(values) {
51
    if (!Array.isArray(values)) {
52
        values = [values];
53
    }
54
    return values;
55
}
56
57
/**
58
 * Turn the output to the correct base.
59
 * @param {!Array<number>} bytes The bytes.
60
 * @param {number} bitDepth The bit depth of the data.
61
 * @param {number} base The desired base for the output data.
62
 */
63
function outputToBase(bytes, bitDepth, base) {
64
    if (bitDepth == 4) {
65
        bytesToBase(bytes, base, pad.paddingNibble);
66
    } else if (bitDepth == 2) {
67
        bytesToBase(bytes, base, pad.paddingCrumb);
68
    } else if(bitDepth == 1) {
69
        bytesToBase(bytes, base, function(){});
70
    }else {
71
        bytesToBase(bytes, base);
72
    }
73
}
74
75
/**
76
 * Write values as bytes.
77
 * @param {!Array<number>|number|string} values The data.
78
 * @param {boolean} isChar True if it is a string.
79
 * @param {boolean} isFloat True if it is a IEEE floating point number.
80
 * @param {number} bitDepth The bitDepth of the data.
81
 * @return {!Array<number>} the bytes.
82
 */
83
function writeBytes(values, isChar, isFloat, bitDepth) {
84
    let bitWriter;
85
    if (isChar) {
86
        bitWriter = writer.writeString;
87
    } else {
88
        bitWriter = writer['write' + bitDepth + 'Bit' + (isFloat ? "Float" : "")];
89
    }
90
    let i = 0;
91
    let j = 0;
92
    let len = values.length;
93
    let bytes = [];
94
    while (i < len) {            
95
        j = bitWriter(bytes, values, i, j);
96
        i++;
97
    }
98
    return bytes;
99
}
100
101
/**
102
 * Write values as bytes.
103
 * @param {!Array<number>} bytes The values.
104
 * @param {boolean} isBigEndian True if the bytes should be big endian.
105
 * @param {number} bitDepth The bitDepth of the data.
106
 */
107
function makeBigEndian(bytes, isBigEndian, bitDepth) {
108
    if (isBigEndian) {
109
        endianness.endianness(bytes, bitDepths.BitDepthOffsets[bitDepth]);
110
    }
111
}
112
113
/**
114
 * Turn bytes to base.
115
 * @param {!Array<string>|!Array<number>} bytes The bytes.
116
 * @param {number} base The base.
117
 * @param {Function} padFunction The function to use for padding.
118
 */
119
function bytesToBase(bytes, base, padFunction=pad.padding) {
120
    if (base != 10) {
121
        let i = 0;
122
        let len = bytes.length;
123
        while (i < len) {
124
            bytes[i] = bytes[i].toString(base);
125
            padFunction(bytes, base, i);
126
            i++;
127
        }
128
    }
129
}
130
131
module.exports.toBytes = toBytes;
132